//
//  ActivityDetailViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2017/12/22.
//  Copyright © 2017年 Lwx. All rights reserved.
//

#import "ActivityDetailViewController.h"
#import "ActivityDetailTableViewCell.h"
#import "BookingInformationViewController.h"

#define border 10
#define headImageH 200
#define labelH 20
#define bottomBarH 50

@interface ActivityDetailViewController ()<ActivityDetailCellDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong)UITableView * tableView;
@property (nonatomic, strong)UIImageView * headImage;
@property (nonatomic, strong)UIView * topView;
@property (nonatomic, strong)UILabel * activityNameLab;
@property (nonatomic, strong)UILabel * moneyLab;
@property (nonatomic, strong)UIView * footView;
@property (nonatomic, strong)UILabel * firstSectionLabel;
@property (nonatomic, strong)UILabel * secondSectionLabel;
@property (nonatomic, strong)UIImageView * detailImage;
@property (nonatomic, strong)UIImageView * adImage;
@property (nonatomic, strong)UIView * bottomBarView;
@property (nonatomic, strong)UIButton * collectBtn;
@property (nonatomic, strong)UIButton * serviceBtn;
@property (nonatomic, strong)UIButton * addTravelBtn;
@end

@implementation ActivityDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomBarView];
    self.navigationController.delegate = self;
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.navigationController.delegate == nil)
    {
        self.navigationController.delegate = self;
    }
}

#pragma mark - UINavigationControllerDelegate
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    BOOL isHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isHomePage animated:YES];
}


- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - bottomBarH)];
        _tableView.backgroundColor = getColor(bgColor);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.tableHeaderView = self.topView;
        _tableView.tableFooterView = self.footView;
//        _tableView.contentSize =
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }return _tableView;
}

- (UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 270)];
        _topView.backgroundColor = [UIColor whiteColor];
        
        UIView * borderView = [[UIView alloc] initWithFrame:CGRectMake(0, _topView.bottom - border, SCREEN_WIDTH, border)];
        borderView.backgroundColor = [UIColor purpleColor];
        borderView.backgroundColor = getColor(bgColor);
        [_topView addSubview:borderView];
        
        [self.topView addSubview:self.headImage];
        [self.topView addSubview:self.activityNameLab];
        [self.topView addSubview:self.moneyLab];
        
        UIButton *back = [[UIButton alloc]initWithFrame:CGRectMake(10, 20, 30, 30)];
        [back setImage:[UIImage imageNamed:@"icon_back2lwx"] forState:UIControlStateNormal];
        [back addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
        [_topView addSubview:back];
        
    }return _topView;
}

- (UIImageView *)headImage{
    if (!_headImage) {
        _headImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, headImageH)];
        _headImage.image = [UIImage imageNamed:@"img_list2111"];

    }return _headImage;
}

- (UILabel *)activityNameLab{
    if (!_activityNameLab) {
        int activityY = self.headImage.bottom + border;
        int activityW = SCREEN_WIDTH - 2 * border;
        _activityNameLab = [[UILabel alloc] initWithFrame:CGRectMake(border, activityY, activityW, labelH)];
        _activityNameLab.text = @"XXXXX活动";
        _activityNameLab.font = DEF_FontSize_14;
        _activityNameLab.textAlignment = NSTextAlignmentLeft;
    }return _activityNameLab;
}

- (UILabel *)moneyLab{
    if (!_moneyLab) {
        _moneyLab = [[UILabel alloc] initWithFrame:CGRectMake(self.activityNameLab.left, self.activityNameLab.bottom, self.activityNameLab.width, self.activityNameLab.height)];
        _moneyLab.text = @"128元";
        _moneyLab.textColor = [UIColor redColor];
        _moneyLab.font = DEF_FontSize_12;
        _moneyLab.textAlignment = NSTextAlignmentLeft;
    }return _moneyLab;
}

- (UIView *)footView{
    if (!_footView) {
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
        _footView.backgroundColor = [UIColor whiteColor];
        UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, labelH)];
        titleLab.centerX = SCREEN_WIDTH / 2;
        titleLab.text = @"活动详情";
        titleLab.font = DEF_FontSize_14;
        titleLab.textAlignment = NSTextAlignmentCenter;
        [_footView addSubview:titleLab];

#warning 活动详情内容，后期确定内容形式来改变高度
        int bottomContentW = SCREEN_WIDTH - 2 * border;
        self.firstSectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(border, titleLab.bottom, bottomContentW, labelH)];
        self.firstSectionLabel.textAlignment = NSTextAlignmentLeft;
        self.firstSectionLabel.text = @"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        self.firstSectionLabel.font = DEF_FontSize_10;
        [_footView addSubview:self.firstSectionLabel];
        
        self.detailImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.firstSectionLabel.left, self.firstSectionLabel.bottom, bottomContentW, headImageH)];
        self.detailImage.image = [UIImage imageNamed:@"img_list2111"];;
        [_footView addSubview:self.detailImage];
        
        self.secondSectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.firstSectionLabel.left, self.detailImage.bottom, bottomContentW, labelH)];
        self.secondSectionLabel.textAlignment = NSTextAlignmentLeft;
        self.secondSectionLabel.text = @"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        self.secondSectionLabel.font = DEF_FontSize_10;
        [_footView addSubview:self.secondSectionLabel];
        
        self.adImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.firstSectionLabel.left, self.secondSectionLabel.bottom, bottomContentW, headImageH / 3)];
        self.adImage.image = [UIImage imageNamed:@"img_list2111"];
        [_footView addSubview:self.adImage];
        
        _footView.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.adImage.bottom + border);
    }return _footView;
}
- (UIView *)bottomBarView{
    if (!_bottomBarView) {
        int collectBtnW = 50;
        _bottomBarView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - bottomBarH, SCREEN_HEIGHT, bottomBarH)];
        _bottomBarView.backgroundColor = [UIColor orangeColor];
        self.collectBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, collectBtnW, _bottomBarView.height)];
        [self.collectBtn setImage:[UIImage imageNamed:@"icon_shoucang"] forState:UIControlStateNormal];
        [self.collectBtn setImage:[UIImage imageNamed:@"icon_yishoucang"] forState:UIControlStateSelected];
        [self.collectBtn addTarget:self action:@selector(collectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.collectBtn setTitle:@"收藏" forState:UIControlStateNormal];
        CGFloat offset = 40.0f;
        self.collectBtn.titleEdgeInsets = UIEdgeInsetsMake(-15, -self.collectBtn.imageView.frame.size.width, -self.collectBtn.imageView.frame.size.height-offset/2, 0);
        self.collectBtn.imageEdgeInsets = UIEdgeInsetsMake(-self.collectBtn.titleLabel.intrinsicContentSize.height +5, -15, 0, -self.collectBtn.titleLabel.intrinsicContentSize.width);
        [self initButton:self.collectBtn];
        self.collectBtn.backgroundColor = [UIColor whiteColor];
        self.collectBtn.adjustsImageWhenHighlighted = NO;
        [_bottomBarView addSubview:self.collectBtn];
        
        self.serviceBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.collectBtn.right, 0, self.collectBtn.width, self.collectBtn.height)];
        [self.serviceBtn setImage:[UIImage imageNamed:@"icon_kefu"] forState:UIControlStateNormal];
        [self.serviceBtn addTarget:self action:@selector(serviceBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.serviceBtn setTitle:@"联系客服" forState:UIControlStateNormal];
        self.serviceBtn.backgroundColor = [UIColor yellowColor];
        
        self.serviceBtn.titleEdgeInsets = UIEdgeInsetsMake(-15, -self.serviceBtn.imageView.frame.size.width, -self.serviceBtn.imageView.frame.size.height-offset/2, 0);
        self.serviceBtn.imageEdgeInsets = UIEdgeInsetsMake(-self.serviceBtn.titleLabel.intrinsicContentSize.height + 8, -30, 0, -self.serviceBtn.titleLabel.intrinsicContentSize.width);
        [self initButton:self.serviceBtn];
        self.serviceBtn.backgroundColor = [UIColor whiteColor];
        [_bottomBarView addSubview:self.serviceBtn];
        
        self.addTravelBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.serviceBtn.right, 0, SCREEN_WIDTH - self.serviceBtn.right, self.collectBtn.height)];
        [self.addTravelBtn setTitle:@"加入行程" forState:UIControlStateNormal];
        [self.addTravelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.addTravelBtn addTarget:self action:@selector(addTravelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.addTravelBtn setBackgroundColor:getColor(@"ffbb02")];
        self.addTravelBtn.titleLabel.font = DEF_FontSize_16;
        self.addTravelBtn.adjustsImageWhenHighlighted = NO;
        [_bottomBarView addSubview:self.addTravelBtn];
        
//        self.collectBtn.backgroundColor = [UIColor yellowColor];
//        self.serviceBtn.backgroundColor = [UIColor blueColor];
    }return _bottomBarView;
}
-(void)initButton:(UIButton*)btn{
//    CGFloat offset = 40.0f;
//    btn.titleEdgeInsets = UIEdgeInsetsMake(-15, -btn.imageView.frame.size.width, -btn.imageView.frame.size.height-offset/2, 0);
//    btn.imageEdgeInsets = UIEdgeInsetsMake(-btn.titleLabel.intrinsicContentSize.height, 0, 0, -btn.titleLabel.intrinsicContentSize.width);
        btn.titleLabel.font = DEF_FontSize_10;
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btn.backgroundColor = [UIColor whiteColor];
}

//自适应Lable长度
- (CGFloat)getWidthWithText:(NSString *)text height:(CGFloat)height font:(CGFloat)font{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height)
                                     options:NSStringDrawingUsesLineFragmentOrigin
                                  attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]}
                                     context:nil];
    return rect.size.width;
}

//自适应Lable高度
- (CGFloat)getLabelHeightWithText:(NSString *)text width:(CGFloat)width font: (CGFloat)font
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil];
    
    return rect.size.height;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"cell";
    [self.tableView registerClass:[ActivityDetailTableViewCell class] forCellReuseIdentifier:cellID];
    ActivityDetailTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    if (!cell) {
        cell = [[ActivityDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.delegate = self;
    return cell;
}

- (void)collectBtnClick:(UIButton *)btn{
    NSLog(@"收藏");
    btn.selected = !btn.selected;
}

- (void)serviceBtnClick{
    NSLog(@"联系客服");
}

- (void)addTravelBtnClick{
    BookingInformationViewController * bookingInformationVC = [[BookingInformationViewController alloc] init];
    [self.navigationController pushViewController:bookingInformationVC animated:YES];
}

- (void)backClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

@end
